﻿// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information.
// Website: https://admin.blazor.zone

using BlazorAdmin.Models;
using BootstrapBlazor.Components;

namespace BlazorAdmin.Web.Shared.Extensions
{
    /// <summary>
    /// 
    /// </summary>
    public static class SelectedItemExtensions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="users"></param>
        /// <returns></returns>
        public static List<SelectedItem> ToSelectedItemList(this IEnumerable<User> users) => users.Select(i => new SelectedItem { Value = i.ID.ToString()!, Text = i.ToString() }).ToList();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        public static List<SelectedItem> ToSelectedItemList(this IEnumerable<Role> roles) => roles.Select(i => new SelectedItem { Value = i.ID.ToString(), Text = i.RoleName }).ToList();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="groups"></param>
        /// <returns></returns>
        public static List<SelectedItem> ToSelectedItemList(this IEnumerable<SysGroup> groups) => groups.Select(i => new SelectedItem { Value = i.ID.ToString(), Text = i.ToString() }).ToList();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dict"></param>
        /// <returns></returns>
        public static List<SelectedItem> ToSelectedItemList(this Dictionary<string, string> dict) => dict.Select(i => new SelectedItem { Value = i.Key, Text = i.Value }).ToList();
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dicts"></param>
        /// <returns></returns>
        public static List<SelectedItem> ToSelectedItemList(this IEnumerable<Dict> dicts) => dicts.Select(i => new SelectedItem { Value = i.Code, Text = i.Name }).ToList();


        public static List<SelectedItem> ToSelectedItemList(this IEnumerable<KeyVal> kvs) => kvs.Select(i => new SelectedItem { Value = i.Key, Text = i.Val }).ToList();

        public static List<SelectedItem> ToIntSelectList(this Type type, SelectedItem? firstItem = null)
        {
            var list = new List<SelectedItem>();
            if (firstItem != null)
                list.Add(firstItem);

            if (type.IsEnum())
            {
                Type _type = Nullable.GetUnderlyingType(type) ?? type;
                string[] names = Enum.GetNames(_type);

                foreach (string text in names)
                {
                    string displayName = Utility.GetDisplayName(_type, text);
                    list.Add(new SelectedItem(((int)Enum.Parse(_type, text)).ToString(), displayName));
                }
            }

            return list;
        }
    }
}